using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Write.Api.DTOs;
using Sheep_Breeding.Write.Api.OperationServices.IServices;
using System;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;

namespace Sheep_Breeding.Write.Api.Controllers
{
    /// <summary>
    /// 羊只销售控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SheepSaleController : ControllerBase
    {
        private readonly ISheepSaleService sheepSaleService;
        private readonly ILogger<SheepSaleController> logger;

        public SheepSaleController(ISheepSaleService sheepSaleService, ILogger<SheepSaleController> logger)
        {
            this.sheepSaleService = sheepSaleService;
            this.logger = logger;
        }

        /// <summary>
        /// 添加羊只销售信息
        /// </summary>
        [HttpPost]
        public async Task<ApiResult> AddSheepSale([FromBody] AddSheepSaleDto dto)
        {
            logger.LogInformation("请求添加羊只销售信息");
            try
            {
                return await sheepSaleService.AddSheepSale(dto);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加羊只销售信息异常: {Message}", ex.Message);
                return new ApiResult { code = ApiREunm.Fail, msg = $"添加失败: {ex.Message}" };
            }
        }

        /// <summary>
        /// 删除羊只销售信息
        /// </summary>
        [HttpDelete]
        public async Task<ApiResult> DeleteSheepSale([FromQuery] DeleteSheepSaleDto dto)
        {
            logger.LogInformation("请求删除羊只销售信息");
            try
            {
                return await sheepSaleService.DeleteSheepSale(dto);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除羊只销售信息异常: {Message}", ex.Message);
                return new ApiResult { code = ApiREunm.Fail, msg = $"删除失败: {ex.Message}" };
            }
        }

        /// <summary>
        /// 更新羊只销售信息
        /// </summary>
        [HttpPut]
        public async Task<ApiResult> UpdateSheepSale([FromBody] UpdateSheepSaleDto dto)
        {
            logger.LogInformation("请求更新羊只销售信息");
            try
            {
                return await sheepSaleService.UpdateSheepSale(dto);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "更新羊只销售信息异常: {Message}", ex.Message);
                return new ApiResult { code = ApiREunm.Fail, msg = $"更新失败: {ex.Message}" };
            }
        }

        /// <summary>
        /// 查询羊只销售信息
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<System.Collections.Generic.List<Sheep_Breeding.Write.Api.DTOs.SheepSaleResultDto>>> SearchSheepSale([FromQuery] SearchSheepSaleDto dto)
        {
            logger.LogInformation("请求查询羊只销售信息");
            try
            {
                var res = await sheepSaleService.SearchSheepSale(dto);
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "查询羊只销售信息异常: {Message}", ex.Message);
                return new ApiResult<System.Collections.Generic.List<Sheep_Breeding.Write.Api.DTOs.SheepSaleResultDto>> { code = ApiREunm.Fail, msg = $"查询失败: {ex.Message}" };
            }
        }

        /// <summary>
        /// 获取羊只销售详情
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<Sheep_Breeding.Write.Api.DTOs.SheepSaleResultDto>> GetSheepSaleById([FromQuery] long id)
        {
            logger.LogInformation($"请求获取羊只销售详情，ID: {id}");
            try
            {
                var res = await sheepSaleService.GetSheepSaleById(id);
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取羊只销售详情异常: {Message}", ex.Message);
                return new ApiResult<Sheep_Breeding.Write.Api.DTOs.SheepSaleResultDto> { code = ApiREunm.Fail, msg = $"获取详情失败: {ex.Message}" };
            }
        }
    }
} 